$(function () {
   new Vue({
        el: "#right",
        data: {
            params: {
                name: "",
                childName: "",
                infUrl: "",
                imgUrl: ""
            }
        },
       mounted:function(){
            if(window.location.hash != "") {
                var id = window.location.hash.substr(1);
                this.getById(id);
                delete id;
            }
       },
        methods: {
            save: function() {
                var self = this;
                var Data = {};
                var url = rootPath + "/module/create-model";
                if(typeof self.params.id != "undefined") {
                    url = rootPath + "/module/update-model";
                }
                for(var key in self.params) {
                    if(self.params[key] == "") {
                        header.Popup('alert',"有必填数据为空","");
                        return
                    } else {
                        Data["obj."+key.substr(0,1).toUpperCase()+key.substr(1)] =   self.params[key];
                    }
                }
                delete key;
                $.ajax({
                    url: url,
                    type: 'POST',
                    data: Data,
                    dataType: 'json',
                    success: function(result) {
                        if(result.status) {
                            header.Popup("alert","操作成功",function(){
                                window.location.href = rootPath + "/module/";
                            });
                        } else {
                            header.Popup("alert",result.error,"");
                        }
                    }
                });
            },
            select: function(element) {
                $('#'+element).click();
            },
            change: function(e) {
                e = e || window.event;
                var obj = e.target || e.srcElement;
                var imgSize = 0;
                var reader = new FileReader();
                var image = new Image();
                if (this.isIE && !obj.files) {
                    var filePath = obj.value;
                    var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
                    var file = fileSystem.GetFile(filePath);
                    imgSize = file.Size;
                } else {
                    imgSize = obj.files[0].size;
                }
                if ((imgSize / 1024) > 2000) {
                    header.Popup("alert","附件不能大于2M","");
                    obj.value = "";
                    return  false;
                }
                var name = obj.value;
                var fileName = name.substring(name.lastIndexOf(".") + 1).toLowerCase();
                if (fileName != "jpg" && fileName != "jpeg" && fileName != "pdf" && fileName != "png" ) {
                    header.Popup("alert","请选择图片格式文件上传(jpg,png,gif,gif等)！","");
                    obj.value = "";
                    return
                }
                reader.readAsDataURL(obj.files[0]);
                reader.onload = function () {
                    image.src = reader.result;
                    image.onload = function(){
                        if(this.width < 80) {
                            header.Popup("alert","上传图片不合规格","");
                            obj.value = "";
                            return
                        }
                        $('.img').find("img").attr("src",reader.result);
                    };

                };
            },
            upload: function(fileElementId) {
                var self = this;
                if($('#'+fileElementId).val() == ""){
                    header.Popup("alert","上传图片不能为空!!!!","");
                    return ;
                }
                $.ajaxFileUpload({
                    url: rootPath+'/img?img_id='+fileElementId+"&img_url="+self.params.imgUrl.substr(1),
                    secureuri: false,
                    fileElementId: fileElementId,
                    type:'post',
                    success: function(data) {
                        var dataStr = $(data).find("body").text();//获取返回的字符串
                        var dataJson = $.parseJSON(dataStr);//把字符串转化为json对象
                        if(dataJson.status) {
                            var callback = function(){
                                self.params.imgUrl = dataJson.data.path.replace(/\\/g, "/");
                                $('.img').find("img").attr('src',rootPath+self.params.imgUrl);
                                $('#'+fileElementId).change(self.change);
                            };
                            header.Popup("alert","上传成功",callback);
                        } else {
                            header.Popup("alert",dataJson.error,"");
                        }
                    }
                });
            },
            imgUrl: function() {
                return this.params.imgUrl == "" ? "" : rootPath + this.params.imgUrl;
            },
            getById: function(id) {
                var self = this;
                $.ajax({
                    url: rootPath+'/module/get-model-by-id',
                    type: 'get',
                    data: {
                        id: id
                    },
                    dataType: 'json',
                    success: function (result) {
                        if(result.status) {
                            self.params = result.data;
                        } else {
                            header.Popup("alert",result.error,"");
                        }
                    }
                });
            }
        }
   }) ;
});
